package com.fingard.dsp.bank.directbank.lchs01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 创兴银行-余额查询接口
 * @author alan
 * @date 2020-06-04
 * @version 1.0
 */
public class LCHS01Balance extends LCHS01Base implements IBalance {

    /**
     * 今日余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //拼接请求报文
                LCHS01Header bankHeader = new LCHS01Header();
                bankHeader.trCode = "0001";
                bankHeader.entSeqNo = p_req7506.reqHeader.transSeqID;
                writeCommonHeader(xmlWriter, bankHeader);
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("accNo", p_req7506.readedDetail.actNum));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                //发送请求并接收响应
                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(), p_req7506.reqHeader.transType, p_req7506.reqHeader.transType);
                //处理响应报文
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ifp/body/errorCode");
                        String returnMsg = tmpBkReader.getFirstText("/ifp/body/errorMsg");
                        if ("0".equals(returnCode)) {
                            while (tmpBkReader.read("/ifp/body/list/coll")){
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = returnMsg;
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("cry"));//币种
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("accNo");//银行账号
                                tmpRetDetail.actArea = p_req7506.readedDetail.actArea;//账号地区区域码
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("accBalance");//余额
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("availBalance");//可用余额
                                tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleTextNotNull("frzBalance");//冻结余额
                            }
                        } else {
                            tmpRetDetail.respCode="-1";
                            tmpRetDetail.respInfo=returnMsg;
                        }
                    } else {
                        tmpRetDetail.respCode="-1";
                        tmpRetDetail.respInfo=tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode="-1";
                    tmpRetDetail.respInfo=tmpRetBkStr[1];
                }
                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts = new RetAts7506BalQry(p_req7506.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("余额查询异常：" + e);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
